﻿using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Gumini.JulaDB.Navigator.Logic;
using Gumini.JulaDB.Provider;
using Microsoft.Win32;

namespace Gumini.JulaDB.Navigator.ViewModels
{
    public class ConnectionWindowViewModel : ObservableObject
    {
        public ConnectionProvider Connection
        {
            get { return SimpleIoc.Default.GetInstance<ConnectionProvider>(); }
        }

        public string FilePath
        {
            get { return _filePath; }
            set
            {
                if (value != _filePath)
                {
                    _filePath = value;
                    RaisePropertyChanged("FilePath");
                    CommandManager.InvalidateRequerySuggested();
                }
            }
        }
        string _filePath;

        public bool IsInMemory
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    RaisePropertyChanged("IsInMemory");
                    CommandManager.InvalidateRequerySuggested();
                }
            }
        }
        bool _name = false;

        private void CloseWindow()
        {
            var currentWindow = Application.Current.Windows.Cast<Window>().SingleOrDefault(x => x.IsActive);
            currentWindow.Close();
        }

        #region Commands

        public Command ConnectCommand
        {
            get
            {
                if (_connectCommand == null)
                    _connectCommand = new Command(Connect);
                return _connectCommand;
            }
        }
        Command _connectCommand;

        private void Connect()
        {
            try
            {
                if (this.IsInMemory)
                    Connection.ConnectToInMemoryDatabase();
                else
                    Connection.ConnectToFileDatabase(this.FilePath);
                CloseWindow();
            }
            catch (JulaDbException ex)
            {
                MessageBox.Show("Unable to connect to database: " + ex.ToString(), "Connection error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public Command CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new Command(CloseWindow);
                return _cancelCommand;
            }
        }
        Command _cancelCommand;

        public Command BrowseCommand
        {
            get
            {
                if (_browseCommand == null)
                    _browseCommand = new Command(Browse, CanBrowse);
                return _browseCommand;
            }
        }
        Command _browseCommand;

        private void Browse()
        {
            var dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == true)
            {
                this.FilePath = dialog.FileName;
            }
        }

        private bool CanBrowse()
        {
            return !this.IsInMemory;
        }

        public Command ClearCommand
        {
            get
            {
                if (_clearCommand == null)
                    _clearCommand = new Command(Clear, CanClear);
                return _clearCommand;
            }
        }
        Command _clearCommand;

        private void Clear()
        {
            this.FilePath = "";
        }

        private bool CanClear()
        {
            return
                !this.IsInMemory &&
                !string.IsNullOrWhiteSpace(this.FilePath);
        }

        #endregion
    }
}
